package beanutils;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import mybatis.vo.Orders;
import net.sf.json.JSONObject;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

public class BeanMapConvert {
	@Test
	public void testMapToBean() throws IllegalAccessException,
			InvocationTargetException {
		Map map = new HashMap();
		map.put("name", "lili");
		map.put("age", 24);
		map.put("birthDay", new Date());
		People p = new People();
		BeanUtils.populate(p, map);
		System.out.println(JSONObject.fromObject(p));
	}

	@Test
	public void testBeanToMap() throws IllegalAccessException,
			InvocationTargetException, NoSuchMethodException {
		People p = new People();
		p.setAge(24);
		p.setBirthDay(new Date());
		p.setName("lili");
		Map map = BeanUtils.describe(p);
		System.out.println(JSONObject.fromObject(map));
	}

	@Test
	public void selectOrderById() {
		String resource = "mybatis/resource/mybatis.xml";
		InputStream inputStream = null;
		try {
			inputStream = Resources.getResourceAsStream(resource);
		} catch (IOException e) {
			e.printStackTrace();
		}
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder()
				.build(inputStream);
		SqlSession session = sqlSessionFactory.openSession();
		try {
			// Order mapper = session.getMapper(Order.class);
			// Order order = Order.selectOrderById(1);
			System.out.println(1);
			Orders order = (Orders) session.selectOne(
					"mybatis.vo.Orders.selectOrderById", 1);
			System.out.println(JSONObject.fromObject(order));
		} finally {
			session.close();
		}
	}
}
